//
//  SetDefaultBankCardViewController.m
//  NewPro
//
//  Created by RYX on 2019/4/22.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "SetDefaultBankCardViewController.h"
#import "SetDefaultBankCardFooterView.h"
#import "BandCardModel.h"
#import "SetDefaultBankCardList.h"
#import "BandCardStore.h"

@interface SetDefaultBankCardViewController ()<UITableViewDelegate,UITableViewDataSource,SetDefaultBankCardFooterViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,assign) NSInteger currentRow;
@property (nonatomic,strong) SetDefaultBankCardFooterView *footerView;
@end

@implementation SetDefaultBankCardViewController

- (SetDefaultBankCardFooterView *)footerView{
    
    if (!_footerView) {
        
        _footerView = [[SetDefaultBankCardFooterView alloc] init];
        _footerView.delegate = self;
    }
    
    return _footerView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _currentRow = 9999;
    
    [self setNavBarWithTitle:@"设置"];
    
    [self.tableView reloadData];
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorInset = UIEdgeInsetsZero;
        _tableView.backgroundColor = UIColor.clearColor;
//        SetDefaultBankCardFooterView *footerView = [[SetDefaultBankCardFooterView alloc] init];
//        footerView.delegate = self;
//        [_tableView setTableFooterView:[footerView initWithFrame:CGRectMake(0, 0, ScreenWidth, 170)]];
        [self.view addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(0);
        }];
    }
    
    return _tableView;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 42;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.listArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BandCardModel *model = self.listArr[indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc] init];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%@（%@）",model.bank,[model.bankNo substringFromIndex:model.bankNo.length - 4]];
    cell.textLabel.textColor = [UIColor colorWithRed:56/255.0 green:56/255.0 blue:56/255.0 alpha:1.0];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_currentRow == 9999) {
        
        if ([model.defaultCard intValue] == 1) {
            
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }else{
            
            cell.accessoryType = UITableViewCellAccessoryNone;
            
        }
        
    }else{
        
        if(_currentRow == indexPath.row){
            
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            
        }else{
            
            cell.accessoryType = UITableViewCellAccessoryNone;
            
        }
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [SetDefaultBankCardList sharedInstance].defaultModel = self.listArr[indexPath.row];
    
    _currentRow = indexPath.row;
    
    [self.tableView reloadData];
    
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return self.footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 170;
}

#pragma mark - 设置默认银行卡
- (void)onSetDefaultBankCardBtn:(UIButton *)sender{
    
    sender.enabled = NO;
    
    MJWeakSelf
    BandCardModel *model = [SetDefaultBankCardList sharedInstance].defaultModel;
    
    if ([model.defaultCard intValue] == 1) {
        
        [weakSelf showMBPError:@"设置成功"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            sender.enabled = YES;
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
        });
        return;
    }
    
    BandCardStore *store = [[BandCardStore alloc] init];
    [store setDefaultBankCardWithId:model.uid Success:^{
        
        if (self.setBankCardBlock != nil) {
            
            self.setBankCardBlock();
        }
        [weakSelf showMBPError:@"设置成功"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            sender.enabled = YES;
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
        });
        
    } Failure:^(NSError *error) {
        
        sender.enabled = YES;
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
}


- (void)returnBlock:(setBankCardBlock)block{
    
    self.setBankCardBlock = block;
}

@end
